package com.ruoyi.wechat.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.httpclient.HttpClientUtil;
import com.ruoyi.common.utils.httpclient.HttpConstant;
import com.ruoyi.common.utils.weather.WeatherDto;
import com.ruoyi.common.utils.weather.WeatherUtils;
import com.ruoyi.wechat.domain.*;
import com.ruoyi.wechat.mapper.WeChatMapper;
import com.ruoyi.wechat.service.AddressCodeService;
import com.ruoyi.wechat.service.SendTemplateMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.ruoyi.wechat.constants.WeChatConstants.*;

/**
 * @author zyj
 * @date 2022-08-22 17:35
 */
@Service
public class SendTemplateMsgServiceImpl implements SendTemplateMsgService {


    @Autowired
    private RedisCache redisCache;

    @Autowired
    private AddressCodeService  addressCodeService;

    @Autowired
    private WeChatMapper weChatMapper;



    /**
     * 获取accessToken
     */
    @Override
    public String getAccessToken(){
        String appId=redisCache.getCacheObject(WECHAT_APP_ID_KEY);
        String appSecret=redisCache.getCacheObject(WECHAT_APP_SECRET_KAY);
        String data = HttpClientUtil.sendHttpGet(String.format(ACCESS_TOKEN_URL, appId, appSecret), HttpConstant.REQ_TIMES);
        WeChatAccessToken weChatAccessToken = JSON.parseObject(data, WeChatAccessToken.class, JSONReader.Feature.SupportSmartMatch);
        redisCache.setCacheObject(WECHAT_ACCESS_TOKEN_KEY,weChatAccessToken.getAccessToken(),weChatAccessToken.getExpiresIn(), TimeUnit.SECONDS);
        return weChatAccessToken.getAccessToken();
    }
    /**
     * 发送模板消息
     *
     * 模板内容：
     * {{nickName.DATA}} 今天是{{date.DATA}} {{title.DATA}}
     * 当前区域:{{city.DATA}}
     * 天气:{{weather.DATA}}
     * 最低气温:{{low.DATA}}
     * 最高气温:{{high.DATA}}
     * {{weatherNotice.DATA}}
     * 今天是我们相识的第{{day.DATA}}天
     * 这{{day.DATA}}天每天都在用心爱你哦
     * {{birthdayMsg.DATA}}
     * 已经在准备礼物啦~
     *  {{footerEnglish.DATA}}
     *  ----------------------------------------
     * {{footer.DATA}}
     */
    @Override
    public void sendTemplateMsg(WeChatUser weChatUser){
        String accessToken = redisCache.getCacheObject(WECHAT_ACCESS_TOKEN_KEY);
        if(accessToken==null){
            accessToken = getAccessToken();
        }
        System.out.println(accessToken);
        TemplateDto<WeatherTemplateData> templateDto =new TemplateDto<WeatherTemplateData>();
        templateDto.setTemplate_id("NEY-8iaim7AFqqAYSGSpLAgIenpG-qafV9vVMlatg9M");
        templateDto.setTouser(weChatUser.getOpenId());
        WeatherTemplateData weatherTemplateData=new WeatherTemplateData();
        WeatherDto weather = WeatherUtils.getWeather(addressCodeService.getAddressCode(weChatUser.getCity()));
        weatherTemplateData.setNickName(new TemplateData(weChatUser.getNickName(),"#E858AF"));
        weatherTemplateData.setWeek(new TemplateData(weather.getWeek(),"#173177"));
        weatherTemplateData.setDate(new TemplateData(DateUtils.getDate(),"#173177"));

        weatherTemplateData.setTitle(new TemplateData("","#173177"));
        weatherTemplateData.setCity(new TemplateData(weChatUser.getCity(),"#173177"));
        weatherTemplateData.setWeather(new TemplateData(weather.getType(),"#3333cc"));
        weatherTemplateData.setWeatherNotice(new TemplateData(weather.getNotice(),"#5471FF"));
        weatherTemplateData.setHigh(new TemplateData(weather.getHigh(),"#FFB554"));
        weatherTemplateData.setLow(new TemplateData(weather.getLow(),"#173177"));
        weatherTemplateData.setDay(new TemplateData(DateUtils.differentDaysByMillisecond(DateUtils.getNowDate(),weChatUser.getAcquaintanceTime())+"","#49F596"));
//        weatherTemplateData.setFooterEnglish(new TemplateData("Don't be discouraged. Everything is for the best. Today also want to work hard refueling oh!",""));
//        weatherTemplateData.setFooter(new TemplateData("别气馁,一切都是最好的安排。今天也要努力加油哦！",""));
        if(weChatUser.getBirthday()!=null){
            weatherTemplateData.setBirthDay(new TemplateData(String.valueOf(DateUtils.getBirthDay(weChatUser.getBirthday())),"#E858AF"));
            weatherTemplateData.setBirthdayMsg(new TemplateData(""+weChatUser.getNickName()+"的生日还有"+DateUtils.getBirthDay(weChatUser.getBirthday())+"天","#E858AF"));
        }
        templateDto.setData(weatherTemplateData);
        HttpClientUtil.sendPostByJson(TEMPLATE_URL+accessToken,JSON.toJSONString(templateDto),HttpConstant.REQ_TIMES);
    }

    @Override
    public void sendAllTemplateMsg(String time) {
        List<WeChatUser> weChatUserList =weChatMapper.selectWechatUserList(time);
        for (WeChatUser weChatUser : weChatUserList) {
            sendTemplateMsg(weChatUser);
        }
    }



}
